import os
import sys
import json
import argparse
import puzautils.table.table as pt
import puzautils.table.puza_row as pr
import puzautils.table.table_utils as ptu

def convert_tweet_file(input_filename, output_filename, block_number):
    input_f = open(input_filename, 'r')
    A = pt.PuzaTable()
    A.OutputFile(output_filename)    
    for lines in input_f:
        w = lines.strip().split('\t')
        if len(w) == 2:
            json_s = w[1]
            #try:
            row = pr.PuzaRow.Load(json_s)            
            A.StreamOutRow(row, pointer = 0, lformat = 1, block_number = block_number)
            #except:
            #continue
    input_f.close()
    A.StreamClose()
    return

def convert_graph_file(input_filename, output_filename, block_number):
    input_f = open(input_filename, 'r')
    A = PuzaMemoryTable()
    for lines in input_f:
        w = lines.strip().split('\t')
        if len(w) == 2:
            source_id = w[0]
            ROW = PuzaRow()
            ROW['source_id'] = source_id
            ROW['target_ids'] = []
            for i in w[1].split(','):
                target_id = i
                ROW['target_ids'].append(target_id)     
            A.Append(ROW)
    input_f.close()
    table_compress_tofile(A, output_filename, block_number)
    return

def convert_tweet_per_directory(dir_name, output_filename = None):
    target_file = os.path.join(dir_name,'tweets.db')
    if os.path.exists(target_file):
        ## there exists a crawled file
        convert_tweet_file(target_file, output_filename)

def convert_tweet_all_directory(starting_dir, output_filename):
    for files in os.listdir(starting_dir):
        if os.path.isdir(files):
            target_dir = os.path.join(starting_dir, files)
            convert_tweet_per_directory(target_dir, )

if __name__ == '__main__':
    usage_str = "usage:\n\
    python convert_crawl_to_db.py [command]\n\
    command --> convert_all_dir [starting_dir] [block (optional)]\n\
    command --> convert_dir [dir] [output file] [block (optional)]\n\
    command --> convert_file [input file] [output file] [block (optional)]\n\
    command --> convert_graph [input_file] [output_file] [block (optional)]"
    if len(sys.argv) <= 1:
        print usage_str
    else:
        command = sys.argv[1].strip()
        if command == 'convert_all_dir':
            if len(sys.argv) < 3:
                print usage_str
            else:
                starting_dir = sys.argv[2]
        elif command == 'convert_dir':
            if len(sys.argv) < 4:
                print usage_str
            else:
                working_dir = sys.argv[2]
                output_file = sys.argv[3]
                block_number = 1
                if len(sys.argv) > 4:
                    block_number = int(sys.argv[4])
                convert_tweet_per_directory(working_dir, output_file)
        elif command == 'convert_file':
            if len(sys.argv) < 4:
                print usage_str
            else:
                input_filename = sys.argv[2]
                output_filename = sys.argv[3]
                block_number = 1
                if len(sys.argv) > 4:
                    block_number = int(sys.argv[4])
                convert_tweet_file(input_filename, output_filename, block_number)
        elif command == 'convert_graph':
            if len(sys.argv) < 4:
                print usage_str
            else:
                input_filename = sys.argv[2]
                output_filename = sys.argv[3]
                block_number = 1
                if len(sys.argv) > 4:
                    block_number = int(sys.argv[4])
                convert_graph_file(input_filename, output_filename, block_number)
        else:
            print usage_str
